package main.java.view;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.util.Observable;
import java.util.Observer;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

import main.java.model.Gears;

public class GearsView extends JPanel implements Observer {
    
    //List of positions
    private static final String [] POSITIONS = {"Up", "Down", "Maneuvering", "Failure"}; 
    
    //static final variables to change positions id easier
    private static final int UP_ID = 0;
    private static final int MANEUVERING_ID = 1;
    private static final int DOWN_ID = 2;
    private static final int FAILURE_ID = 3;

    //static final variables of view dimension to modify them easier
    private static final int SIZE1 = 200;
    
    //Path of each image of the gears
    private static String gearsUpImage = "images/GearsUp.png";
    private static String gearsDownImage = "images/GearsDown.png";
    private static String gearsManeuveringImage = "images/GearsManeuvering.png";
    private static String gearsFailureImage = "images/GearsFailure.png";
    
    private Image gearsImage;
    
    /**
     * Constructor GearsView
     */
    public GearsView() {
        //Set to up
        this.gearsImage = new ImageIcon(gearsUpImage).getImage(); 
        
        //Initialize panel info
        Dimension size = new Dimension(gearsImage.getWidth(null), gearsImage.getHeight(null));
        this.setPreferredSize(size);
        this.setMinimumSize(size);
        this.setMaximumSize(size);
        this.setSize(size);
        this.setLayout(null);
    }
    
    /**
     * Switch the image depending on the position of the gears
     * @param position : position of the gears
     */
    public void switchImage(String position) {
        switch (position) {
            //If up then set gearsUpImage
            case "Up": 
                this.gearsImage = new ImageIcon(gearsUpImage).getImage();
                break;
            //If down then set gearsDownImage
            case "Down": 
                this.gearsImage = new ImageIcon(gearsDownImage).getImage();
                break;
            //If maneuvering then set gearsManeuveringImage
            case "Maneuvering": 
                this.gearsImage = new ImageIcon(gearsManeuveringImage).getImage();
                break;
            //If failure then set gearsFailureImage
            case "Failure":
                this.gearsImage = new ImageIcon(gearsFailureImage).getImage();
                break;
            default:
                break;
        }
        //repaint
        paintImmediately(0, 0, SIZE1, SIZE1); 
    }

    /**
     * Draw image
     */
    public void paintComponent(Graphics g) {
        g.drawImage(gearsImage, 0, 0, null);
    }

    /**
     * Update image 
     * Depending on the position of the gears
     * @param Observable : what type of instance
     * @param Object : given change 
     */
    @Override
    public void update(Observable o, Object arg) {
        if (o instanceof Gears) {
            switch (arg.toString()) {
                case "Up":
                    switchImage(POSITIONS[UP_ID]);
                    break;
                case "Down":
                    switchImage(POSITIONS[MANEUVERING_ID]);
                    break;
                case "Maneuvering":
                    switchImage(POSITIONS[DOWN_ID]);
                    break;
                case "Failure":
                    switchImage(POSITIONS[FAILURE_ID]);
                    break;
                default:
                    break;
            }
        }
    }
}
